package cn.edu.xmu.oomall.customer.service;

import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.customer.dao.AddressDao;
import cn.edu.xmu.oomall.customer.microservice.FreightService;
import cn.edu.xmu.oomall.customer.microservice.vo.SimpleRegionRetVo;
import cn.edu.xmu.oomall.customer.model.bo.Address;
import cn.edu.xmu.oomall.customer.model.vo.AddressRetVo;
import cn.edu.xmu.oomall.customer.model.vo.AddressVo;
import cn.edu.xmu.privilegegateway.annotation.util.InternalReturnObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.cloneVo;

/**
 * @author Jiawei Zheng
 * @date 2021-12-09
 */
@Service
public class AddressService {

    @Autowired
    AddressDao addressDao;

    @Autowired
    private FreightService freightService;

    /**
     * 买家新增地址
     * @param userId
     * @param userName
     * @param addressVo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ReturnObject addAddress(Long userId, String userName, AddressVo addressVo){
        //先获取买家所有地址，看是否达到地址上限（20）
        ReturnObject<List<Address>> returnObject1 = addressDao.getAllAddresses(userId, 1, 30);
        if(returnObject1.getData()!=null){
            long total = returnObject1.getData().size();
            if(total>=20){
                return new ReturnObject(ReturnNo.ADDRESS_OUTLIMIT);
            }
        }
        //新增地址
        Address address = cloneVo(addressVo, Address.class);
        address.setCustomerId(userId);
        ReturnObject<Address> returnObject = addressDao.addAddress(userId, userName, address);
        if(returnObject.getData()==null){
            return returnObject;
        }
        Address bo = returnObject.getData();
        bo.setBeDefault(false);
        AddressRetVo retVo = cloneVo(bo, AddressRetVo.class);
        InternalReturnObject<SimpleRegionRetVo> internalReturnObject = freightService.getRegionInfo(bo.getRegionId());
        if(internalReturnObject.getData()==null){
            return new ReturnObject(internalReturnObject);
        }
        retVo.setRegion(new SimpleRegionRetVo(bo.getRegionId(),internalReturnObject.getData().getName()));
        return new ReturnObject(retVo);
    }

    /**
     * 买家获取地址列表
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Transactional(rollbackFor = Exception.class, readOnly = true)
    public ReturnObject getAllAddresses(Long userId, Integer page, Integer pageSize){
        ReturnObject<List<Address>> returnObject = addressDao.getAllAddresses(userId, page, pageSize);
        if(returnObject.getData()==null){
            return returnObject;
        }
        List<Address> boList = returnObject.getData();
        List<AddressRetVo> retVoList = new ArrayList<>();
        for(Address bo:boList){
            AddressRetVo addressRetVo = cloneVo(bo, AddressRetVo.class);
            InternalReturnObject<SimpleRegionRetVo> internalReturnObject = freightService.getRegionInfo(bo.getRegionId());
            if(internalReturnObject.getData()==null){
                return new ReturnObject(internalReturnObject);
            }
            addressRetVo.setRegion(new SimpleRegionRetVo(bo.getRegionId(),internalReturnObject.getData().getName()));
            retVoList.add(addressRetVo);
        }
        return new ReturnObject(retVoList);
    }

    @Transactional(rollbackFor = Exception.class)
    public ReturnObject setDefaultAddress(Long userId, String userName, Long id){
        return addressDao.setDefaultAddress(userId, userName, id);
    }

    /**
     * 买家修改地址信息
     * @param userId
     * @param userName
     * @param id
     * @param addressVo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ReturnObject updateAddress(Long userId, String userName, Long id, AddressVo addressVo){
        Address address = cloneVo(addressVo, Address.class);
        return addressDao.updateAddress(userId, userName, id, address);
    }

    /**
     * 买家删除地址
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ReturnObject deleteAddress(Long userId, Long id){
        return addressDao.deleteAddress(userId, id);
    }
}
